package cn.buddha.jec.db.service.impl;

import cn.buddha.jec.db.dao.FlowableInfoMapper;
import cn.buddha.jec.db.entity.FlowableInfo;
import cn.buddha.jec.db.service.IFlowableInfoService;
import cn.buddha.jec.flowable.constant.FlowableStatus;
import cn.buddha.jec.model.TongBaseRequest;
import cn.buddha.jec.model.TongBaseResponse;
import cn.buddha.jec.model.TongContext;
import cn.buddha.jec.util.TongDateUtils;
import cn.buddha.jec.util.TongExceptionConfigUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务流程信息表(VERTICAL) 服务实现类
 * </p>
 *
 * @author 彤彤
 * @since 2020-04-01
 */
@Service
@Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRES_NEW)
@Slf4j
public class FlowableInfoServiceImpl extends ServiceImpl<FlowableInfoMapper, FlowableInfo> implements IFlowableInfoService {

    /**
     * @return
     * @Author 友野浩二
     * @Description // 服务流程信息保存
     * @Date 2020/4/2 0:28
     * @Param [input, flowableClassName]
     */
    @Override
    public void saveFlowableInfo(TongBaseRequest input) {
        TongContext tongContext = TongContext.getInstance();

        FlowableInfo flowableInfo = new FlowableInfo();
        flowableInfo.setFlowableId(tongContext.getFlowableId());// 服务流程编号
        flowableInfo.setFlowableUri(tongContext.getReqUrl());// URI
        flowableInfo.setFlowableClassName(tongContext.getReqClassName());// 服务流程类名
        flowableInfo.setReqSystemId(tongContext.getTongInHead().getReqSystemId());// 服务流程请求方系统编号
        flowableInfo.setReqTranDate(tongContext.getTongInHead().getReqTranDate());// 服务流程请求方交易日期(yyyyMMdd)
        flowableInfo.setReqBusinessNo(tongContext.getTongInHead().getReqBusinessNo());// 服务流程请求方流水号
        flowableInfo.setTranDate(TongDateUtils.convertStr2Date(tongContext.getTranDate(), TongDateUtils.PATTERN_SIMPLE_DATE));// 服务流程交易时间
        flowableInfo.setStartTime(TongDateUtils.createNowTimestamp());// 服务流程开始时间戳
        flowableInfo.setFlowableStatus(FlowableStatus.STATUS_START);// 服务流程执行状态: 开始执行状态
        String inStr = JSON.toJSONString(input);
        flowableInfo.setFlowableInMsg(getMessage(inStr));// 服务流程请求数据

        // 业务逻辑处理之前公共处理的异常开关
        if (TongExceptionConfigUtils.tongExPreHandlerEnable) {
            // 故意制造异常
            int a = 1 / 0;
            log.info("呵呵了", a);
        }

        // 服务流程信息保存
        this.save(flowableInfo);
    }

    /**
     * @return
     * @Author 友野浩二
     * @Description // 更新服务流程信息
     * @Date 2020/4/9 16:41
     * @Param [output, flowableStatus(成功-FlowableStatus.STATUS_SUCCESS,异常-FlowableStatus.STATUS_EXCEPTION)]
     */
    @Override
    public void updateFlowableInfo(TongBaseResponse output, String flowableStatus) {
        TongContext tongContext = TongContext.getInstance();

        FlowableInfo flowableInfo = new FlowableInfo();
        flowableInfo.setFlowableId(tongContext.getFlowableId());// 服务流程编号
        flowableInfo.setFlowableStatus(flowableStatus);// 服务流程执行状态: 执行异常状态
        flowableInfo.setEndTime(TongDateUtils.createNowTimestamp());// 服务流程结束时间
        String exceptionStr = JSON.toJSONString(output);
        flowableInfo.setFlowableOutMsg(getMessage(exceptionStr));// 服务流程响应数据

        // 服务流程信息更新
        this.updateById(flowableInfo);
    }

    private String getMessage(final String message) {
        if (message.length() > 2000) {
            return message.substring(0, 2000);
        }
        return message;
    }
}
